package com.nnfe.fyd.feature.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nnfe.fyd.R;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.CountDownTimerUtils;
import com.nnfe.fyd.utils.DialogUtil;
import com.nnfe.fyd.utils.StringUtil;
import com.nnfe.fyd.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/16.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class ResetPwdFragment extends ActionBarFragment {
    @Bind(R.id.et_phone_number)
    EditText etPhoneNumber;
    @Bind(R.id.et_captcha)
    EditText etCaptcha;
    @Bind(R.id.tv_bt_capthca)
    TextView tvBtCapthca;
    @Bind(R.id.et_psd)
    EditText etPsd;
    @Bind(R.id.et_psd_again)
    EditText etPsdAgain;
    @Bind(R.id.btn_next)
    Button btnNext;
    private String phoneNum;
    private String captcha;
    private String password;

    @Override
    public Object getTitleString() {
        return "忘记密码";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_reset_pwd;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_bt_capthca, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bt_capthca:
                if (checkCanSendCaptcha()) {
                    sendCaptcha();
                }
                break;
            case R.id.btn_next:
                if (checkCanReset()) {
                    resetPwd();
                }
                break;
        }
    }

    private void resetPwd() {
        DialogUtil.showLoadingDialog(mContext, "修改中,请稍后...");
        Call<HttpResult<String>> call = service.resetPwd(phoneNum, captcha, password, password);
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                if (response.body().getStatus() == 0) {
                    ToastUtil.showToast("修改成功,请重新登录。");
                    mContext.onBackPressed();
                }
            }

            @Override
            protected void onRequestFinish() {
                super.onRequestFinish();
                DialogUtil.dismissLoadingDialog(mContext);
            }
        });

    }

    private boolean checkCanReset() {

        phoneNum = etPhoneNumber.getText().toString().trim();
        captcha = etCaptcha.getText().toString().trim();
        password = etPsd.getText().toString().trim();
        String passwordAgain = etPsdAgain.getText().toString().trim();

        if (!StringUtil.isMobile(phoneNum)) {
            ToastUtil.showToast("请输入正确的手机号!");
            return false;
        }

        if (TextUtils.isEmpty(captcha)) {
            ToastUtil.showToast("验证码不能为空");
            return false;
        }

        if (TextUtils.isEmpty(password) || TextUtils.isEmpty(passwordAgain)) {
            ToastUtil.showToast("密码不能为空");
            return false;
        }

        if (password.length() < 8) {
            ToastUtil.showToast("密码输入长度不正确");
            return false;
        }

        if (passwordAgain.length() < 8) {
            ToastUtil.showToast("确认密码输入长度不正确！");
            return false;
        }
        if (!password.equals(passwordAgain)) {
            ToastUtil.showToast("两次密码输入不一致！");
            return false;
        }

        return true;
    }

    private void sendCaptcha() {
        Call<HttpResult<String>> call = service.sendCaptcha(phoneNum);
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                if (response.body().getStatus() == 0) {
                    ToastUtil.showToast("验证码发送成功！");
                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvBtCapthca, 60000, 1000);
                    countDownTimerUtils.start();
                }
            }
        });
    }


    /**
     * 验证是否符合发送验证码条件
     *
     * @return
     */
    private boolean checkCanSendCaptcha() {
        phoneNum = etPhoneNumber.getText().toString().trim();
        if (!StringUtil.isMobile(phoneNum)) {
            ToastUtil.showToast("请输入正确的手机号");
            return false;
        }

        return true;
    }
}
